package com.sysu.ooad.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.sysu.ooad.dao.CommonDao;
import com.sysu.ooad.dao.TaskDao;
import com.sysu.ooad.entity.Course;
import com.sysu.ooad.entity.Student;
import com.sysu.ooad.entity.Task;

@Component("taskDao")
public class TaskDaoImpl extends CommonDao implements TaskDao {
	public void add(Task task) {
		hibernateTemplate.save(task);
	}

	@SuppressWarnings("unchecked")
	public List<Task> findByCourse(Course c) {
		List<Task> tasks = (List<Task>)hibernateTemplate.find("from Task t where t.course = ? order by t.publishTime desc", new Object[]{c});
		return tasks;
	}

	@SuppressWarnings("unchecked")
	public Task findByPositionOrderByPublishTime(int pos, Course c) {
		List<Task> tasks = (List<Task>)hibernateTemplate.find("from Task t where t.course = ? order by t.publishTime", new Object[]{c});
		return tasks.get(pos);
	}

	@SuppressWarnings("unchecked")
	public List<Task> findNotCloseTasksByCourse(Course c) {
		return (List<Task>)hibernateTemplate.find("from Task t where t.course = ? and t.deadline > ?", new Object[]{c, new Date()});
	}

	public Task findById(Integer id) {
		return (Task)hibernateTemplate.get(Task.class, id);
	}

	public int countByCourse(Course c) {
		Long count = (Long)hibernateTemplate.find("select count(t.id) from Task t where t.course = ?", new Object[]{c}).get(0);
		return count.intValue();
	}

	public void update(Task task) {
		hibernateTemplate.update(task);
	}




}
